package com.starinside.wp.controller.api;

import com.starinside.wp.config.WxMpProperties;
import com.starinside.wp.domain.TimeLineItem;
import com.starinside.wp.domain.WxUser;
import com.starinside.wp.domain.liveRecord.RecordListVo;
import com.starinside.wp.domain.record.LiveRecord;
import com.starinside.wp.foundation.model.ErrorCode;
import com.starinside.wp.foundation.model.StatusResponse;
import com.starinside.wp.foundation.query.base.PagerQuery;
import com.starinside.wp.service.BindedBabyService;
import com.starinside.wp.service.LiveRecordService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;

import static com.starinside.wp.foundation.constant.RequestConstant.TICKET;

/**
 * @author Binary Wang(https://github.com/binarywang)
 */
@EnableConfigurationProperties(WxMpProperties.class)
@RestController
@RequestMapping("/wx/api/school")
@Api(value = "/wx/api/school", tags = "班级动态，时光轴")
public class APIClassCircleController {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private LiveRecordService liveRecordService;

    @Autowired
    private BindedBabyService bindedBabyService;

    /**
     * 园区动态接口
     * @param req
     * @param resp
     * @param query
     * @return
     */
    @RequestMapping(value = "/classcircle", method = {RequestMethod.GET, RequestMethod.POST})
    @ApiOperation(value = "获取当前小朋友班级动态")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "页码"),
            @ApiImplicitParam(name = "pageSize", value = "页容量")
    })
    public StatusResponse getClassCircleAPI(HttpServletRequest req, HttpServletResponse resp,
                                            @RequestHeader(TICKET) String ticket,
                                            PagerQuery query){
        this.logger.info("api get classcircle");

        WxUser loginUser = (WxUser) req.getSession().getAttribute("loginUser");

        String[] childrenIds = bindedBabyService.getAllBindedBabyIdsByParentId(loginUser.getParentId());

        if(childrenIds == null || childrenIds.length == 0){
            return StatusResponse.error(ErrorCode.CHILDREN_NOT_EXIST, "木找到宝宝");
        }

        Page<LiveRecord> recordPage = liveRecordService.getLiveRecordsByChildrenIds(childrenIds, query.getPageNum(), query.getPageSize());

        return StatusResponse.success(recordPage);
    }

    /**
     * 时光轴 api
     * @param req
     * @param resp
     * @param baseTime
     * @param pageSize
     * @return
     */
    @RequestMapping(value = "/timeline", method = {RequestMethod.GET, RequestMethod.POST})
    @ApiOperation(value = "获取当前小朋友在园时光轴")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "baseTime", value = "基准时间，分页时，查询此时间之前的至少页容量指定条数的记录"),
            @ApiImplicitParam(name = "pageSize", value = "页容量")
    })
    public StatusResponse getLiveRecordsAPI(HttpServletRequest req, HttpServletResponse resp, String baseTime, Integer pageSize){
        this.logger.info("api get liveRecords api");

        WxUser loginUser = (WxUser) req.getSession().getAttribute("loginUser");

        if(StringUtils.isEmpty(loginUser.getDefaultBabyId())){
            return StatusResponse.error(ErrorCode.CHILDREN_NOT_EXIST, "木找到宝宝");
        }


        if(pageSize == null || pageSize < 10){
            pageSize = 10;
        }

        if(StringUtils.isEmpty(baseTime)){
            Date d = new Date();
            baseTime = d.toString();
        }

        List<TimeLineItem> items = liveRecordService.getTimeLineItemsByChildrenId(loginUser.getDefaultBabyId(), baseTime, pageSize);

        return StatusResponse.success(items);
    }

}
